﻿//AcademicPlanner - course registration planning web app.
//Copyright (C) 2009  Boubacar Diallo

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AcademicPlanner.Data
{
	/// <summary>
	/// List of application level error codes in the data layer of the
	/// AcademicPlanner application.
	/// </summary>
	/// <since>1.0</since>
	public enum DataErrorCode
	{
		NONE,
		SAVE_ENTITY_FAILED,
		GET_ENTITY_FAILED,
		GET_NAMED_QUERY_FAILED,
		QUERY_PERSON_ENTITY_BY_EMAIL_FAILED,
		QUERY_ACCOUNT_ENTITY_BY_USER_ENTITY_NAME_FAILED,
		QUERY_PERSON_ENTITY_BY_ACCOUNT_ENTITIES_USER_ENTITY_NAME_FAILED,
		CREATE_SQL_QUERY_FAILED,
		CREATE_FILTER_QUERY_FAILED,
		REFRESH_ENTITY_FAILED,
		QUERY_PLANNER_ENTITY_BY_USER_ENTITY_NAME_FAILED,
		QUERY_COURSE_ENTITY_FOR_ACADEMICPLAN_ENTITY_FAILED,
		FILTER_ENTITY_BY_ID_FAILED,
		QUERY_ACADEMICSCHEDULE_ENTITY_FOR_ACADEMICPLAN_ENTITY_FAILED,
		QUERY_REGISTRATION_ENTITY_BY_PERIODENTITY_YEAR_AND_PERIODENTITY_TERMENTITY_SEQORDER_FAILED
	}

	/// <summary>
	/// DataException represents an application level exception in the
	/// data layer of the AcademicPlanner application.
	/// </summary>
	/// <since>1.0</since>
	public class DataException : ApplicationException
	{
		DataErrorCode errorCode;


		/// <summary>
		/// The error code associated with this DataException.
		/// </summary>
		/// <since>1.0</since>
		public DataErrorCode ErrorCode { get { return errorCode; } }

		/// <summary>
		/// Gets the error message associated with this DataException.
		/// </summary>
		public override string Message { get { return GetErrorCodeMessage(errorCode) + " - " + InnerException.Message; } }


		/// <summary>
		/// Constructs a DataException with the given <c>DataErrorCode</c> and
		/// <c>InnerException</c>.
		/// </summary>
		/// <param name="pCode">The <c>DataErrorCode</c>.</param>
		/// <param name="pInnerException">The <c>InnerException</c>.</param>
		/// <since>1.0</since>
		public DataException(DataErrorCode pCode, Exception pInnerException) : base("", pInnerException)
		{
			errorCode = pCode;
		}

		/// <summary>
		/// Gets the error message associated with a given <c>DataErrorCode</c>.
		/// </summary>
		/// <param name="pCode">The <c>DataErrorCode</c>.</param>
		/// <returns>The error message.</returns>
		/// <since>1.0</since>
		public static string GetErrorCodeMessage(DataErrorCode pCode)
		{
			// TODO: RETURN MORE COMPREHENSIBLE MESSAGE
			return pCode.ToString();
		}
	}
}
